//
//  StringUtils.m
//  JQBV2
//
//  Created by dev on 14-8-22.
//  Copyright (c) 2014年 EFunds. All rights reserved.
//

#import "StringUtils.h"

@implementation StringUtils

+(CGFloat)getHeightWithString:(NSString *)string
                        width:(CGFloat)width
                         font:(UIFont *)font
                        color:(UIColor *)color{
    
    NSString *str = [StringUtils emptyStringReplaceNSNull:string];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attrStr addAttribute:NSFontAttributeName
                    value:font
                    range:NSMakeRange(0,str.length)];
    if (color) {
        [attrStr addAttribute:NSForegroundColorAttributeName
                        value:color
                        range:NSMakeRange(0,str.length)];
    }
    
    CGFloat titleHeight;
    
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                        options:options
                                        context:nil];
    titleHeight = ceilf(rect.size.height);
    
    return titleHeight + 2;
}

#pragma mark nil
+(NSString *)getString:(id)string{
    return [NSString stringWithFormat:@"%@",string];
}
+ (BOOL)isEmptyString:(id)string{
    return ![string isKindOfClass:[NSString class]] || [(NSString *)string length]==0 || [(NSString *)string isEqualToString:@"(null)"];
}

+(NSString *)emptyStringReplaceNSNull:(id)string{
    if ([self isEmptyString:string]) {
        return @"";
    }else{
        return string;
    }
}

+(NSString *)zeroReplayEmptyString:(id)string
{
    if ([self isEmptyString:string]) {
        return @"0";
    }else{
        return string;
    }
}

#pragma mark format

+(NSString *)numberFormat:(NSInteger)num{
    return [NSString stringWithFormat:@"数目：%ld",(long)num];
}

+(NSString *)amountMarkFormat:(NSString *)string{
    return [NSString stringWithFormat:@"￥%@",[self amountFormat:string]];
}

+(NSString *)amountFormat:(id)string{
    NSString *resultStr = nil;
    if ([self isEmptyString:string]) {
        resultStr = @"0.00";
    }else if([self isValidateMoney:string]){
        NSNumberFormatter *numFormat = [[NSNumberFormatter alloc] init];
        [numFormat setPositiveFormat:@"###,##0.00;"];
        NSNumber *num = [NSNumber numberWithDouble:[string doubleValue]];
        resultStr = [numFormat stringFromNumber:num];
    }else{
        resultStr = string;
    }
    return resultStr;
}

+(BOOL)isValidateMoney:(NSString *)money
{
    NSString *regex = @"^([\\-+]?)([1-9][0-9]{0,}(\\.[0-9]{0,})?)$|^([\\-+]?)(0(\\.[0-9]{0,})?)$";;
    NSPredicate *prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [prd evaluateWithObject:money];
}

+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [prd evaluateWithObject:email];
}

+ (BOOL)isValidateIDCard:(NSString *)IDCard
{
    NSString *regex = @"^\\d{14}[\\d|x|X]$|^\\d{17}[\\d|x|X]$";
    NSPredicate *IDCardPre=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [IDCardPre evaluateWithObject:IDCard];
}


+ (BOOL)areStringEqualWithStr1:(NSString *)str1 str2:(NSString *)str2
{
    if (str1==nil && str2==nil) {
        return YES;
    }
    if (str2!=nil) {
        return [str1 isEqualToString:str2];
    }
    return NO;
}

#pragma mark save

+(NSDictionary *)getUserDefaultsObjForKey:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

+(void)setUserDefaultsObj:(NSDictionary *)obj forKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults] setObject:obj forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark urlCodedString

+(NSString*)urlEncodeString:(NSString*)unencodedString{
    
    NSString *encodedString = @"";
    
    if (![StringUtils isEmptyString:unencodedString]) {
        encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)unencodedString, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"),kCFStringEncodingUTF8));
    }
    
    return encodedString;
}

//URLDEcode
+(NSString*)urlDecodeString:(NSString*)encodedString{
    
    //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    
    NSString *decodedString = (__bridge_transfer NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,(__bridge CFStringRef)encodedString,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    return decodedString;
}

#pragma mark date

+(NSDate *)timeIntervalChangeToData:(NSString *)timeInterval{
    return [NSDate dateWithTimeIntervalSince1970:[timeInterval doubleValue]];
}

//图片、视频、文件等路径不显示 UTF8编码
+(NSString *)getUTLString:(NSString *)url{
    return [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}


@end
